﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class DAO_GiangVien
    {
        public static List<DTO_GiangVien> SelectGiangVien()
        {
            List<DTO_GiangVien> listGiangVien = new List<DTO_GiangVien>();
            try
            {
                string s = "Proc_SelectGiangVien";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = ketnoi.Cmb();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = s;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataTable dt = new DataTable();
                da.Fill(dt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DTO_GiangVien GV = new DTO_GiangVien();
                    GV.MaGV = dt.Rows[i]["MaGV"].ToString();
                    GV.TenGV = dt.Rows[i]["TenGV"].ToString();
                    GV.DienThoai = dt.Rows[i]["DienThoai"].ToString();
                    if ((bool)dt.Rows[i]["GioiTinh"] == false)
                        GV.GioiTinh = false;
                    else
                        GV.GioiTinh = true;
                    
                    GV.Email = dt.Rows[i]["Email"].ToString();
                    GV.MaCN = dt.Rows[i]["MaCN"].ToString();
                    GV.CNBoMon = dt.Rows[i]["CNBoMon"].ToString();
                    GV.LVCN = dt.Rows[i]["LVCN"].ToString();
                    GV.MaKhoa = dt.Rows[i]["MaKhoa"].ToString();
                    listGiangVien.Add(GV);
                }
                ketnoi.Cmb().Close();
                return listGiangVien;
            }
            catch (Exception ex)
            {
                return listGiangVien;
            }
        }
        public static bool deleteGiangVien(string MaGV)
        {
            List<DTO_GiangVien> listGiangVien = new List<DTO_GiangVien>();
           
            try
            {
                string s = "Proc_deleteGiangVien";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = ketnoi.Cmb();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = s;
                cmd.Parameters.Add("@MaGV", SqlDbType.NVarChar).Value = MaGV;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataTable sv = new DataTable();
                da.Fill(sv);
                for (int i = 0; i < sv.Rows.Count; i++)
                {
                    DTO_GiangVien giangvien = new DTO_GiangVien();
                    giangvien.MaGV = sv.Rows[i]["MaGV"].ToString();
                    listGiangVien.Add(giangvien);
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
        public static bool insertGiangVien(DTO_GiangVien GiangVien)
        {
            try
            {
                SqlConnection cn = ketnoi.Cmb();
                string s = "Proc_InsertGiangVien";
                SqlCommand cmd1 = new SqlCommand(s, cn);
                cmd1.CommandType = CommandType.StoredProcedure;
                cmd1.Parameters.AddWithValue("@MaGV", GiangVien.MaGV);
                cmd1.Parameters.AddWithValue("@TenGV", GiangVien.TenGV);
                cmd1.Parameters.AddWithValue("@DienThoai", GiangVien.DienThoai);
                cmd1.Parameters.AddWithValue("@GioiTinh", GiangVien.GioiTinh);
                cmd1.Parameters.AddWithValue("@Email", GiangVien.Email);
                cmd1.Parameters.AddWithValue("@MaCN", GiangVien.MaCN);
                cmd1.Parameters.AddWithValue("@CNBoMon", GiangVien.CNBoMon);
                cmd1.Parameters.AddWithValue("@LVCN", GiangVien.LVCN);
                cmd1.Parameters.AddWithValue("@MaKhoa", GiangVien.MaKhoa);

                if (cn.State == ConnectionState.Closed)
                    cn.Open();
                cmd1.ExecuteNonQuery();
                DTO_TaiKhoan tk = new DTO_TaiKhoan();
                tk.TenTK = GiangVien.MaGV;
                tk.MatKhau = GiangVien.MaGV;
                tk.ID_Nhom = 2;
                DAO_TaiKhoan.InsertDangNhap_VaoTaiKhoan(tk);

            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }


        public static bool updateGiangVien(DTO_GiangVien GiangVien, string MaGV_cu)
        {
            try
            {
                SqlConnection cn = ketnoi.Cmb();
                string s = "Proc_UpdateGiangVien";
                SqlCommand cmd1 = new SqlCommand(s, cn);
                cmd1.CommandType = CommandType.StoredProcedure;
                cmd1.Parameters.AddWithValue("@MaGV", GiangVien.MaGV);
                cmd1.Parameters.AddWithValue("@TenGV", GiangVien.TenGV);
                cmd1.Parameters.AddWithValue("@DienThoai", GiangVien.DienThoai);
                cmd1.Parameters.AddWithValue("@GioiTinh", GiangVien.GioiTinh);
                cmd1.Parameters.AddWithValue("@Email", GiangVien.Email);
                cmd1.Parameters.AddWithValue("@MaCN", GiangVien.MaCN);
                cmd1.Parameters.AddWithValue("@CNBoMon", GiangVien.CNBoMon);
                cmd1.Parameters.AddWithValue("@LVCN", GiangVien.LVCN);
                cmd1.Parameters.AddWithValue("@MaKhoa", GiangVien.MaKhoa);
                cmd1.Parameters.AddWithValue("@MaGV_Cu", MaGV_cu);
                if (cn.State == ConnectionState.Closed)
                    cn.Open();
                cmd1.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
        public static List<DTO_GiangVien> SelectGV_TheoMaGV(string magv)
        {
            List<DTO_GiangVien> listgv = new List<DTO_GiangVien>();
            try
            {
                string s = "Proc_SelectGiangVienTheoMaGV";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = ketnoi.Cmb();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = s;
                cmd.Parameters.Add("@MaGV", SqlDbType.NVarChar).Value = magv;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataTable dt = new DataTable();
                da.Fill(dt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DTO_GiangVien gv = new DTO_GiangVien();
                    gv.MaGV = dt.Rows[i]["MaGV"].ToString();
                    gv.TenGV = dt.Rows[i]["TenGV"].ToString();
                    gv.DienThoai = dt.Rows[i]["DienThoai"].ToString();
                    gv.GioiTinh = bool.Parse(dt.Rows[i]["GioiTinh"].ToString());
                    gv.Email = dt.Rows[i]["Email"].ToString();
                    gv.MaCN = dt.Rows[i]["MaCN"].ToString();
                    gv.CNBoMon = dt.Rows[i]["CNBoMon"].ToString();
                    gv.LVCN = dt.Rows[i]["LVCN"].ToString();
                    gv.MaKhoa = dt.Rows[i]["MaKhoa"].ToString();
                    listgv.Add(gv);
                }
                ketnoi.Cmb().Close();
                return listgv;
            }
            catch (Exception ex)
            {
                return listgv;
            }
        }
        public static bool Update_TTGV(DTO_GiangVien GV, string magv)
        {
            try
            {
                SqlConnection cn = ketnoi.Cmb();
                string s = "Update_TTGV";
                SqlCommand cmd1 = new SqlCommand(s, cn);
                cmd1.CommandType = CommandType.StoredProcedure;
                cmd1.Parameters.AddWithValue("@Email", GV.Email);
                cmd1.Parameters.AddWithValue("@DienThoai", GV.DienThoai);
                cmd1.Parameters.Add("@MaGV", SqlDbType.NVarChar).Value = magv;
                if (cn.State == ConnectionState.Closed)
                    cn.Open();
                cmd1.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
